package com.zhuang.designpattern.observer;

import com.zhuang.designpattern.observer.event.listener.EventManager;
import com.zhuang.designpattern.observer.event.listener.MQEventListener;
import com.zhuang.designpattern.observer.event.listener.MessageEventListener;

/**
 * description: LotteryService
 * date: 2022/9/20 16:45
 * author: Zhuang
 * version: 1.0
 */
public abstract class LotteryService {

    private final EventManager eventManager;

    protected LotteryService() {
        eventManager = new EventManager(EventManager.EventType.MQ, EventManager.EventType.MESSAGE);
        eventManager.subscribe(EventManager.EventType.MQ, new MQEventListener());
        eventManager.subscribe(EventManager.EventType.MESSAGE, new MessageEventListener());
    }

    public LotteryResult draw(String uId) {
        LotteryResult lotteryResult = doDraw(uId);
        // 需要什么通知就给调用什么方法
        eventManager.notify(EventManager.EventType.MQ, lotteryResult);
        eventManager.notify(EventManager.EventType.MESSAGE, lotteryResult);
        return lotteryResult;
    }

    protected abstract LotteryResult doDraw(String uId);
}
